/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.viweb.storemanager.entity;

import com.viweb.storemanager.core.PropertyIndex;
import com.viweb.storemanager.core.SafeObject;
import java.text.Collator;
import java.text.RuleBasedCollator;
import java.util.Locale;

/**
 *
 * @author nhchung
 */
public class Customer extends BaseEntity implements Comparable, Cloneable, SafeObject, PropertyIndex {
    //khai báo biến

    protected Object cloner;
    private String name;
    private TypeCustomer type;
    private String address;
    private String email;
    private String phone;
    private String accountNumber;
    private String bank;

    //khai báo hàm khởi tạo
    public Customer() {
    }

    public Customer(int id, String name) {
        setId(id);
        setName(name);
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the type
     */
    public TypeCustomer getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(TypeCustomer type) {
        this.type = type;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return the accountNumber
     */
    public String getAccountNumber() {
        return accountNumber;
    }

    /**
     * @param accountNumber the accountNumber to set
     */
    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    /**
     * @return the bank
     */
    public String getBank() {
        return bank;
    }

    /**
     * @param bank the bank to set
     */
    public void setBank(String bank) {
        this.bank = bank;
    }

    public Object getCloner() {
        return cloner;
    }

    public boolean validate() {

        if (this.getType() == null) {
            this.setValidDescription("Chưa chọn loại khách hàng.");
            return false;
        }

        if (this.getName().equals("")) {
            this.setValidDescription("Tên khách hàng không được để rỗng");
            return false;
        }

        if (this.getName().length() > 100) {
            this.setValidDescription("Tên khách hàng không vượt quá 100 ký tự.");
            return false;
        }
        return true;
    }
    //khai báo phương thức

    public Object getPropertyValue(int index) {
        String value = "";
        switch (index) {
            case 1:
                value = String.valueOf(getId());
                break;
            case 2:
                value = getName();
                break;
            case 3:
                value = getType().getName();
                break;
            case 4:
                value = getAddress();
                break;
            case 5:
                value = getEmail();
                break;
            case 6:
                value = getPhone();
                break;
            case 7:
                value = getAccountNumber();
                break;
            case 8:
                value = getBank();
                break;
            case 9:
                value = getDatemodifies().toString();
                break;
            case 10:
                value = String.valueOf(getNote());
                break;
            case 11:
                value = getNote();
                break;
        }
        return value;
    }

    public void setPropertyValue(int index, Object value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Class getPropertyClass(int index) {
        return String.class;
    }

    public int compareTo(Object object) {
        if (!(object instanceof Customer)) {
            throw new ClassCastException("Ngoại lệ: đối tượng khách hàng không hợp lệ!");
        }

        RuleBasedCollator viCollator = (RuleBasedCollator) Collator.getInstance(new Locale("vi"));
        return viCollator.compare(this.getName(), ((Customer) object).getName());
    }

    public void backup() {
        try {
            this.cloner = this.clone();
        } catch (CloneNotSupportedException ex) {
        }
    }

    public void restore() {
        this.setName(((Customer) getCloner()).getName());
        this.setAddress(((Customer) getCloner()).getAddress());
        this.setBank(((Customer) getCloner()).getBank());
        this.setEmail(((Customer) getCloner()).getEmail());
        this.setPhone(((Customer) getCloner()).getPhone());
        this.setType(((Customer) getCloner()).getType());
    }

    @Override
    public String toString() {
        return getName();
    }
}
